package consapi

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
)

// ParseEventRequest 解析事件数据
func ParseEventRequest(req *http.Request) (*Event, error) {
	return ParseEventReadCloser(req.Body)
}

// ParseEventReadCloser 解析事件数据
func ParseEventReadCloser(r io.ReadCloser) (*Event, error) {
	data, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, fmt.Errorf("读取数据失败:%v", err)
	}
	return ParseEventBytes(data)
}

// ParseEventBytes 解析事件数据
func ParseEventBytes(data []byte) (evt *Event, err error) {
	err = json.Unmarshal(data, &evt)
	return
}
